package com.wontube.smr.remote.web;

import java.util.List;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.sites.youtube.feed.YouTubeFeedsBO;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.client.youtube.IYoutubeFeedHandler;
import com.wontube.smr.dao.bo.GenreSelectDao;
import com.wontube.smr.model.vo.YoutubeFeedParams;
import com.wontube.smr.web.client.IFeaturesHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;


@NoLoginSupport
public class FeaturesHandler extends BasicHandler implements IFeaturesHandler {	
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> listYoutubeGenres() {
		return GenreSelectDao.selectGenres(Genre.CATE_SHORT_FILM);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Movie getShortFilm(String filmId) {				
		if (filmId.startsWith("yt")) {
			filmId = filmId.substring("yt".length());
		}
		
		return YouTubeFeedsBO.getSingle(filmId);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listRelated(String filmId, PagenateInfo pageInfo) {
		if (StringUtils.isNullOrEmpty(filmId)) {
			return new MovieItems();
		}		
		
		if (filmId.startsWith("yt")) {
			filmId = filmId.substring("yt".length());
		}
		
		return YouTubeFeedsBO.listRelated(filmId, pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listYoutubeMostViewed(QueryItemParams params,
			PagenateInfo pageInfo) {
		String period = params.getPeriod();
		period = (period == null) ? IYoutubeFeedHandler.TIME_TODAY : period;
		MovieItems items = YouTubeFeedsBO.selectStandardFeeds(YoutubeFeedParams.STANDARD_FEED_MOST_VIEWED, 
				period, params.getGenre(), pageInfo);		
		if(params.isNeedGenres()){
			items.setGenres(listYoutubeGenres());
		}
		return items;
	}	
}
